﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Http;
using WebAPI.Models;
using CommonLib.Extended;
using EntityModel;

namespace WebAPI.Controllers.V1
{
    /// <summary>
    /// 微信管理
    /// </summary>
    [System.Web.Http.RoutePrefix("Api/V1/system_menu")]
    public class V3_system_menuController : WebAPI.Models.ApiBase
    {
        #region 微信管理

        /// <summary>
        /// 查询:微信管理
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [Validation(false), Results(false), HttpPost, Route("Getmenu"), Description("查询:微信管理")]
        public EntityModel.V1.system_menu.Response.Select Getmenu(EntityModel.V1.system_menu.Request.Basic user)
        {
			Expression<Func<EntityModel.system_menu, bool>> strWhere = ex => ex.IsDelete == (int)EntityModel.Enum.IsDelete.Normal;
			strWhere=strWhere.And(ex => ex.ID == user.ID);
            var entity = DBContext.ExSelect<EntityModel.system_menu>(strWhere).FirstOrDefault();
            var obj = new EntityModel.V1.system_menu.Response.Select();
            CommonLib.EntityHelper.SetBase(entity, obj);

            obj.IsDelete_Name = GetDicName(2, obj.IsDelete);
            obj.AddUserID_Name = GetUserInfo(obj.AddUserID).user_name;
            obj.LastUserId_Name = GetUserInfo(obj.LastUserId).user_name;
            return obj;
        }

        /// <summary>
        /// 添加:微信管理
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Validation(true), Results(false), HttpPost, Route("Addmenu"), Description("添加:微信管理")]
        public int Addmenu(EntityModel.V1.system_menu.Request.AddUpdate entity)
        {
            var obj = new EntityModel.system_menu(entity.Header);
            obj.SetV3Base(entity);
            obj.AddUserID = entity.Header.LoginCache.UserInfoID;
            obj.LastUserId = entity.Header.LoginCache.UserInfoID;
            DBContext.ExAdd(obj);

            return obj.ID;
        }

        /// <summary>
        /// 编辑:微信管理
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Validation(true), Results(false), HttpPost, Route("Updatemenu"), Description("编辑:微信管理")]
        public int Updatemenu(EntityModel.V1.system_menu.Request.AddUpdate entity)
        {
            var obj = DBContext.ExSelect<EntityModel.system_menu>(ex => ex.ID == entity.ID).FirstOrDefault();
            obj.SetV3BaseUpdate(entity);

            obj.LastTime = DateTime.Now;
            obj.LastUserId = entity.Header.LoginCache.UserInfoID;
            return DBContext.ExUpdate(obj);
        }

        /// <summary>
        /// 删除:微信管理
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Validation(true), Results(false), HttpPost, Route("Deletemenu"), Description("删除:微信管理")]
        public int Deletemenu(EntityModel.V1.system_menu.Request.Basic entity)
        {
            var obj = DBContext.ExSelect<EntityModel.system_menu>(ex => ex.ID == entity.ID).FirstOrDefault();
            obj.IsDelete = (int)EntityModel.Enum.IsDelete.Delete;
            return DBContext.ExUpdate(obj);
        }

        /// <summary>
        /// 分页:微信管理
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [Validation(true), Results(true), HttpPost, Route("GetPagesmenu"), Description("分页:微信管理")]
        public WebAPI.Models.ApiPagesResult<EntityModel.V1.system_menu.Response.Select> GetPagesmenu(EntityModel.V1.system_menu.Request.SelectPages search)
        {
            var result = new WebAPI.Models.ApiPagesResult<EntityModel.V1.system_menu.Response.Select>();
            var where = base.GetWhere<dd_project>(search.Filter);//查询处理逻辑

            var list = DBContext.ExPage<EntityModel.system_menu>(where, search.Page);
            var obj = new List<EntityModel.V1.system_menu.Response.Select>();
            CommonLib.EntityHelper.SetBase(list.ToList(), obj);

            foreach (var item in obj)
            {
                item.IsDelete_Name = GetDicName(2, item.IsDelete);
                item.AddUserID_Name = GetUserInfo(item.AddUserID).user_name;
                item.LastUserId_Name = GetUserInfo(item.LastUserId).user_name;
            }

            result.Page = search.Page;
            result.Results = obj;
            return result;
        }
        #endregion
    }

}
